/**
 * Created by Administrator on 2017/3/27 0027.
 */
var indexModel = (function () {
    return {
        initFrameHeight : function () {
            var window_height = $(window).height();
            var header_height = $('.main-header').outerHeight();
            var footer_height = $('.main-footer').outerHeight();
            $("#mainFrame").height(window_height - header_height - footer_height-10);
            //$("#mainFrame").contents().find("body").css('min-height', window_height - $('.main-footer').outerHeight());
        }

    }
})();

var sysDict;

(function () {
	//系统ctx
	window.path=$("#ctx").val();
	
	template.helper('ctx', function () {
	    return path;
	});
	
    indexModel.initFrameHeight();
  	//加载系统字典表
    initSysDict();
	//获取用户信息
	getSessionUser();
	//获取用户菜单信息
	getUserMenu();
	//重置密码按钮弹出层
	$("#btnResetPwd").click(showResetPwdFormDialog);
})();

//获取用户信息
function getSessionUser(){
	var userInfo;
	var userInfoStr = sessionStorage.getItem('userInfo');
	if(userInfoStr){
		userInfo = JSON.parse(userInfoStr);
	}
	if(!userInfo){	
		$.get(path+'/api/user/userInfo',function(resData){
			userInfo = resData.payload;
			$("#username1").text(userInfo.user_name);
			$("#username2").text(userInfo.user_name);
			$("#username3").text(userInfo.user_name);
			//存到sessionStorage
	    	sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
		})
	}else{
			$("#username1").text(userInfo.user_name);
			$("#username2").text(userInfo.user_name);
			$("#username3").text(userInfo.user_name);
	}
}

//获取用户菜单信息
function getUserMenu(){
	var userMenu;
	var userMenuStr = sessionStorage.getItem('userMenu');
	if(userMenuStr){
		userMenu = JSON.parse(userMenuStr);
	}
	if(!userMenu){
		$.get(path+'/api/user/userMenus',function(resData){
			userMenu = resData.payload;
			var menuHtml = template('tpl-menu-list', {menuData : userMenu});
			$("#tsMenu").html(menuHtml);
			//存到sessionStorage
	    	sessionStorage.setItem('userMenu', JSON.stringify(userMenu));
		})
	}else{
		var menuHtml = template('tpl-menu-list', {menuData : userMenu});
		$("#tsMenu").html(menuHtml);
	}
}

//加载系统字典表
function initSysDict(){
	$.get(path+'/api/dictInfos',function(resData){
		var payload;
		if(resData.payload){
			payload=resData.payload;
		}else{
			payload=[];
		}
		
		var map = {}, destArry = [];
		//按照group_code分组
		$.each(payload,function(index,item){
			    if(!map[item.group_code]){
			    	destArry.push({
			    		group_code: item.group_code,
			    		group_name: item.group_name,
			            datas: [item]
			        });
			        map[item.group_code] = item;
			    }else{
			        for(var i = 0; i < destArry.length; i++){
			            var di = destArry[i];
			            if(di.group_code == item.group_code){
			            	di.datas.push(item);
			                break;
			            }
			        }
			    }
		})
		
		$.each(destArry,function(index,item){
			if(item.datas.length > 1){
				//按照 item_order 升序排列
				item.datas.sort(function(a,b){
					return a.item_order - b.item_order;
				})
			}
		})
		
		sysDict=destArry;
	})
}

//重置密码按钮弹出层
function showResetPwdFormDialog(){
	customRemoteUrlDialog('重置密码','/page/business/sysmanage/user/resetPwdForm',null);
}